package cn.com.nxt.txb.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import cn.com.nxt.txb.BaseActivity;
import cn.com.nxt.txb.BuildConfig;
import cn.com.nxt.txb.HttpProtocol;
import cn.com.nxt.txb.ResponseStatus;
import cn.com.nxt.txb.util.AppConstant;
import cn.com.nxt.txb.widget.LoadingDialog;

/**
 * Created by lt on 2017/5/19.
 */

public class BaseFragment extends Fragment implements HttpProtocol {
    private LoadingDialog dialog = null;
    public Toast mToast = null;
    public SharedPreferences.Editor editor;
    public SharedPreferences sp;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getActivity().getSharedPreferences(AppConstant.USERINFO, getActivity().MODE_PRIVATE);
        editor = sp.edit();
    }
    public static void showLog(String msg) {
        if (BuildConfig.DEBUG) {
            Log.e("sdydbj", msg);
        }
    }

    public void showToast(String msg) {
        if (BuildConfig.DEBUG) {
            if (mToast == null) {
                mToast = Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(msg);
                mToast.setDuration(Toast.LENGTH_SHORT);
            }
            mToast.show();
        }
    }

    public void showToast(int msg) {
        if (BuildConfig.DEBUG) {
            if (mToast == null) {
                mToast = Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(msg);
                mToast.setDuration(Toast.LENGTH_SHORT);
            }
            mToast.show();
        }
    }


    @Override
    public void success(String result, byte flag) {

    }

    @Override
    public void failure(String result, byte flag, ResponseStatus status) {

    }

    @Override
    public void finished(int flag) {

    }

    @Override
    public void cancelled(int flag) {

    }

    public void back(View v) {
        getActivity().finish();
    }
    // 环形加载进度条
    public void showDialog() {
        if (dialog != null) {
            dialog = null;
        }
        dialog = new LoadingDialog(getActivity());
        dialog.setCancelable(true);
        dialog.show();
    }

    public void showDialog(String msg) {
        dialog = new LoadingDialog(getActivity(), msg);
        dialog.show();
    }

    public void disDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
